package com.atguigu.create.singleton.type3;

/**
 * 单例模式-懒汉式(线程不安全)
 */
public class SingletonTest3 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1==instance2);
        System.out.println("instance1:"+instance1.hashCode());
        System.out.println("instance2:"+instance2.hashCode());
    }
}

class Singleton{
    // 1. 私有化构造器
    private Singleton() {
    }

    // 2. 创建一个private static对象
    private static Singleton singleton;

    // 3. getInstance(懒汉式-线程不安全)
    public static Singleton getInstance(){
        if(singleton==null){
            singleton=new Singleton();
        }
        return singleton;
    }

}
